package com.yingpai.fitness.dvp_2_0.main.activity.mine.integral;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.activity.vip.BuyVipCardActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.vip.VipCardActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.IsSuccessBean;
import com.yingpai.fitness.dvp_2_0.main.bean.vip.VipCardDetailBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.vip_card.MemberInfoBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 积分购买会员卡
 * Created by ASUS on 2018/12/19.
 */

public class IntegralMallVipCardActivity extends CommonActivity {


    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.title_tv)
    TextView title_tv;
    @BindView(R.id.integral)
    TextView integral;
    @BindView(R.id.content)
    TextView content;
    private String id;
    private VipCardDetailBean mVipCardDetailBean;
    private TokenBean tokenBean;
    private DecimalFormat df = new DecimalFormat("######.###");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_integral_mall_vip_card);
    }

    @Override
    @OnClick({R.id.right_tv, R.id.bottom_tv})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.right_tv:
                break;
            case R.id.bottom_tv:
//                DialogUtil.getInstance().showCenterDialog(this, R.layout.dialog_shop_pay_success);
//                View dialog_logout = DialogUtil.getInstance().dialog_center_layout;
//                dialog_logout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        DialogUtil.getInstance().dialog_center.dismiss();
//                    }
//                });
//                TextView title = dialog_logout.findViewById(R.id.title);
//                title.setText("确定使用"+mVipCardDetailBean.getData().getExchangePoint()+"积分兑换？");
//                dialog_logout.findViewById(R.id.message).setVisibility(View.GONE);
//                dialog_logout.findViewById(R.id.sure_cancel).setVisibility(View.VISIBLE);
//                TextView sure = dialog_logout.findViewById(R.id.sure);
//                TextView cancel = dialog_logout.findViewById(R.id.cancel);
//                cancel.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        DialogUtil.getInstance().dialog_center.dismiss();
//                    }
//                });
//                sure.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        DialogUtil.getInstance().dialog_center.dismiss();
//                        Bundle bundle = new Bundle();
////                        startActivity(BuyVipCardActivity.class);
//                    }
//                });
                showDialog(this, "");
                HttpParams httpParams = new HttpParams();
                httpParams.put("customerId", tokenBean.getId());
                finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_INFORMATION,
                        httpParams, Url.GET_INFORMATION, new Callback<MemberInfoBean>() {
                            @Override
                            public void onSuccess(MemberInfoBean mMemberInfo) {
                                dismissDialog();
                                if (Constant.REQUEST_SUCCESS.equals(mMemberInfo.getResult())) {
                                    if (mMemberInfo.getMap().getCustomer().isIsMemeber()) {
                                        HashMap<String,Object> map = new HashMap<>();
                                        map.put("id",mVipCardDetailBean.getData().getId());
                                        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT,
                                                Contacts.HOST_SERVER + Url.EXCHANGE_VIP_CARD, map, Url.EXCHANGE_VIP_CARD,
                                                new Callback<IsSuccessBean>() {
                                                    @Override
                                                    public void onSuccess(IsSuccessBean isSuccessBean) {
                                                        dismissDialog();
                                                        if(isSuccessBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                                                            EventBus.getDefault().post("==exchange==");
                                                            startActivity(VipCardActivity.class);
                                                            finishActivity();
                                                        }else{
                                                            showToast(isSuccessBean.getMessage()+"");
                                                        }
                                                    }

                                                    @Override
                                                    public void onError(Response<String> response) {
                                                        super.onError(response);
                                                        dismissDialog();
                                                    }
                                                });
                                    } else {
                                        Bundle bundle = new Bundle();
                                        bundle.putString("id",mVipCardDetailBean.getData().getId());
                                        bundle.putString("integral",mVipCardDetailBean.getData().getExchangePoint()+"");
                                        bundle.putString("cardPrice",df.format(mVipCardDetailBean.getData().getCardPrice()/100));
                                        bundle.putSerializable("infomation", mMemberInfo);
                                        startActivity(BuyVipCardActivity.class, bundle);
                                    }
                                } else {
                                    showToast(mMemberInfo.getMsg());
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                            }
                        });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void initView() {
        setTitle("商品详情");
        EventBus.getDefault().register(this);
        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            id = bundle.getString("id");
        }
//        Drawable drawableLeft = getResources().getDrawable(
//                R.mipmap.share);
//        rightTv.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
//                null, null, null);
    }

    @Override
    protected void initData() {
        showDialog(this,null);
        HashMap<String,Object> params = new HashMap();
        params.put("id",id);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.INTEGRAL_MALL_VIP_DETAIL, params, Url.INTEGRAL_MALL_VIP_DETAIL, new Callback<VipCardDetailBean>() {
            @Override
            public void onSuccess(VipCardDetailBean vipCardDetailBean) {
                if(vipCardDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                    mVipCardDetailBean = vipCardDetailBean;
                    money.setText("￥"+df.format(vipCardDetailBean.getData().getCardPrice()/100));
                    title_tv.setText(vipCardDetailBean.getData().getName());
                    integral.setText(vipCardDetailBean.getData().getExchangePoint()+"");
                    content.setText(vipCardDetailBean.getData().getCardInfo().getTitle() + "\n" + vipCardDetailBean.getData().getCardInfo().getInfo());
                }else{
                    showToast(vipCardDetailBean.getMessage()+"");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String event) {
        if ("finish".equals(event)) {
            finishActivity();
        }
    }
}
